package com.ltu.service.impl;


import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ltu.domain.BaseEntity;
import com.ltu.model.request.base.BaseFilterPageReq;
import com.ltu.model.request.base.BaseIdReq;
import com.ltu.model.response.base.CodeDataResp;
import com.ltu.myannotation.MyReidsCache;
import com.ltu.service.BaseTService;
import com.ltu.util.common.MpPageUtil;
import com.ltu.util.common.StrUtils;

import springfox.documentation.annotations.ApiIgnore;

@CrossOrigin(origins="*",allowedHeaders="*")
@ApiIgnore
	public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity>  extends ServiceImpl<M, T>  implements BaseTService<T>{
//	public class BaseController<M extends BaseMapper<T>, T>  extends ServiceImpl<M, T>  implements BaseTService<T>{

	protected  static String getLikeStr(JSONObject  param,String prefix) {
		
		StringBuilder likeIng = new StringBuilder(" ");
		
		Object[]  keys =	 param.keySet().toArray();
		
		if(keys.length > 0)
			likeIng.append(" ( ");
		for (int i=0;i<keys.length;i++) {
			String likeKey =  keys[i].toString();
			 if(StringUtils.isBlank(param.getString(likeKey)))
				 continue;
			if (i>0 )
					likeIng.append(" or ");
			String column =prefix.concat(StrUtils.humpToUnderline(likeKey));			
				likeIng.append( column + " like '%" + param.getString(likeKey) + "%'");				
		}
		if(keys.length > 0)
			likeIng.append(" ) ");
		return likeIng.toString();
		
	}
	
	@Override
	public CodeDataResp<Page<T>> getMePage(BaseFilterPageReq<T> req) {
		Page<T> page = MpPageUtil.getCommonPage(req.getPageReq());
		QueryWrapper<T>  t=new QueryWrapper<T>();
		
		if(null != req.getKeyWords()&& req.getKeyWords().size() > 0) 
			t.apply(getLikeStr(req.getKeyWords(),""));
		 t.setEntity(req.getEntityParam());
    	Page<T>	 result= super.page(page, t);
		return CodeDataResp.valueOfSuccess(result);
	}

	@Override
	public CodeDataResp<List<T>> getMeList(BaseFilterPageReq<T> req) {
		QueryWrapper<T>  t=new QueryWrapper<T>();
		
			if(null != req.getKeyWords() && req.getKeyWords().size() > 0) 
				t.apply(getLikeStr(req.getKeyWords(),""));
			 t.setEntity(req.getEntityParam());
		return CodeDataResp.valueOfSuccess(super.list(t));
	}
	

	@Override
	public CodeDataResp edit(T t) {
		super.saveOrUpdate(t);
		return   CodeDataResp.valueOfSuccessEmptyData();
	}

	@Override
	public CodeDataResp<T> getDetail(BaseIdReq req) {
		return CodeDataResp.valueOfSuccess(super.getById(req.getId()));
	}

	@Override
	public CodeDataResp remove(BaseIdReq req) {
		// TODO Auto-generated method stub
		return CodeDataResp.valueOfSuccess(super.removeById(req.getId()));
	}
	
	
}
